package com.mune.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mune.common.exception.BizCodeEnum;
import com.mune.common.exception.NoStockException;
import com.mune.common.to.SkuHasStockVo;
import com.mune.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mune.gulimall.ware.entity.WareSkuEntity;
import com.mune.gulimall.ware.service.WareSkuService;
import com.mune.common.utils.PageUtils;
import com.mune.common.utils.R;


/**
 * 商品库存
 *
 * @author mune
 * @email 1356008591@qq.com
 * @date 2020-07-29 10:38:12
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
  @Autowired
  private WareSkuService wareSkuService;

  @PostMapping("/hasstock")
  public R getSkusHasStock(@RequestBody List<Long> skuIds) {
    List<SkuHasStockVo> skusHasStock = wareSkuService.getSkusHasStock(skuIds);
    return R.ok().setData(skusHasStock);
  }

  @PostMapping("/lock/order")
  public R orderLockStock(@RequestBody WareSkuLockVo vo) {
    try {
      Boolean stock = wareSkuService.orderLockStock(vo);
      return R.ok().setData(stock);
    } catch (NoStockException e) {
      return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(), BizCodeEnum.NO_STOCK_EXCEPTION.getMessage());
    }

  }

  /**
   * 列表
   */
  @RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = wareSkuService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  public R info(@PathVariable("id") Long id) {
    WareSkuEntity wareSku = wareSkuService.getById(id);

    return R.ok().put("wareSku", wareSku);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody WareSkuEntity wareSku) {
    wareSkuService.save(wareSku);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody WareSkuEntity wareSku) {
    wareSkuService.updateById(wareSku);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] ids) {
    wareSkuService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
